package enron;

import jade.core.Agent;
import jade.domain.FIPAAgentManagement.FailureException;
import jade.domain.FIPAAgentManagement.NotUnderstoodException;
import jade.domain.FIPAAgentManagement.RefuseException;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.proto.ContractNetResponder;

public class AdjustGeneratorBehaviour extends ContractNetResponder {
	private static final long serialVersionUID = 5494249538558227262L;

	public AdjustGeneratorBehaviour(Agent a, MessageTemplate mt) {
		super(a, mt);
	}

	/* (non-Javadoc)
	 * @see jade.proto.ContractNetResponder#handleAcceptProposal(jade.lang.acl.ACLMessage, jade.lang.acl.ACLMessage, jade.lang.acl.ACLMessage)
	 */
	@Override
	protected ACLMessage handleAcceptProposal(ACLMessage cfp,
			ACLMessage propose, ACLMessage accept) throws FailureException {
		ACLMessage response = accept.createReply();
		//System.out.println(myAgent.getLocalName() + ": adjusting by " + cfp.getContent() + "kW");
		if(((VendorAgent)myAgent).adjustGenerators(Long.parseLong((cfp.getContent())))) {
			response.setPerformative(ACLMessage.INFORM);
			//System.out.println(myAgent.getLocalName() + ": adjusted to " + ((VendorAgent)myAgent).getPower() + "kw");
		} else {
			response.setPerformative(ACLMessage.FAILURE);
			//System.out.println(myAgent.getLocalName() + ": did not adjust, still " + ((VendorAgent)myAgent).getPower() + "kw");
		}
		return response;
	}

	/* (non-Javadoc)
	 * @see jade.proto.ContractNetResponder#handleCfp(jade.lang.acl.ACLMessage)
	 */
	@Override
	protected ACLMessage handleCfp(ACLMessage cfp) throws RefuseException,
			FailureException, NotUnderstoodException {
		ACLMessage proposal = cfp.createReply();
		proposal.setPerformative(ACLMessage.PROPOSE);
		proposal.setContent(((VendorAgent)myAgent).getAdjustmentCost(Long.parseLong((cfp.getContent()))).toString());
		//System.out.println(myAgent.getLocalName() + ": cost is " + proposal.getContent());
		return proposal;
	}
}
